package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class DelayTimeHandler implements MsgHandler {
    @Autowired
    private OrderService   orderService;
    @Override
    public void process(String jsonMsg) throws IOException {
           //解析数据
        String orderNo = JsonUtil.getByJson(jsonMsg, String.class);
        // 根据订单编号查询订单状态
        if(orderNo.equals("")){
            return;
        }
    OrderEntity  orderEntity=  orderService.getOne(Wrappers.<OrderEntity>lambdaQuery().eq(OrderEntity::getOrderNo, orderNo));
        if (orderEntity == null) {
            throw  new LogicException("该订单不存在");
        }
            Integer status = orderEntity.getStatus();
     if(status==null)
     {
         throw  new LogicException("该订单状态为空");
     }
        if(status== VMSystem.ORDER_STATUS_CREATE){
            //未支付，则更改该订单为无效
            orderEntity.setStatus(VMSystem.ORDER_STATUS_INVALID);
            orderService.updateById(orderEntity);
        }
    }
}
